package com.itheima.udpdeom;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPServerdemo {
    public static void main(String[] args) throws Exception {
        System.out.println("服务器启动");
        //1.创建服务器端对象，注册端口
        DatagramSocket server = new DatagramSocket(8080);
        //2.创建数据包对象
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        while (true) {
            //3.接收数据
            server.receive(packet);
            //4.解析数据，且获取对方的ip地址和端口号
            String msg = new String(buffer, 0, packet.getLength());
            String ip = packet.getAddress().getHostAddress();
            int port = packet.getPort();
            System.out.println("客户端发送的数据为：" + msg + "。ip地址为：" + ip + "端口号为：" + port);
        }
    }
}
